
class UserScene extends Scene {
  private change_head: eui.Group;
  private username: eui.Label;
  private change_name: eui.Group;
  private ID: eui.Label;
  private change_sex: eui.Group;
  private sex: eui.Label;
  private age: eui.Label;
  private change_age: eui.Group;
  private dept: eui.Label;
  private change_Dept: eui.Group;
  private age_changing: eui.Image;
  private dept_changing: eui.Image;
  private radio1: eui.RadioButton;
  private radio2: eui.RadioButton;
  private head_pic: eui.Image;//头像图片
  private userBack: eui.Image;
  //private age_input: eui.TextInput;//年龄输入文本框
  private nowdept_Img: eui.Image;
  private drop_Img: eui.ToggleButton;
  private datalist: eui.List;
  private nowdept_label: eui.Label;
  private namechanging_bg: eui.Image;
  private nameBack: eui.Image;
  private finishBtn: eui.Button;
  private name_input: eui.TextInput;
  private namechanging_Group: eui.Group;
  private deptchanging_Group: eui.Group;
  private sexchanging_Group: eui.Group;
  private agechanging_Group: eui.Group;
  private depts: any[];
  private male_Group:eui.Group;
  private female_Group:eui.Group;
  private sex_changing: eui.Image;
  private userHelper: UserHelper = UserHelper.instance;
  private userInfo: EtreeUserInfo;
  private ageArr:any[];
  private agelist:eui.List;
  private depts_Group:eui.Group;
  public constructor() {
    super();
    this.depts = new Array();
    this.depts.push({ label: "计算机学院" });
    this.depts.push({ label: "设计艺术学院" });
    this.depts.push({ label: "语言文学学院" });
    this.depts.push({ label: "土木工程学院" });
    this.depts.push({ label: "数理学院" });
    this.depts.push({ label: "医学院" });
    this.depts.push({ label: "护理学院" });
    this.depts.push({ label: "其他学院" });

  }
  public async onSkinLoaded() {
    //圆形头像的遮罩
        var shp:egret.Shape=new egret.Shape();       
        shp.graphics.beginFill(0x0000ff);
        shp.graphics.drawCircle(605.5,236.5,60.5);
        shp.graphics.endFill();
        this.addChild(shp);
        this.head_pic.mask=shp;

        this.ageArr=[];
        for(var i:number=0;i<=100;i++){
          this.ageArr.push({label:i});
        }      
        this.agelist.dataProvider=new eui.ArrayCollection(this.ageArr);
        this.agelist.itemRenderer=AgeRenderer;
    this.change_sex.addEventListener(egret.TouchEvent.TOUCH_TAP, this.btnHandler, this);
    this.change_age.addEventListener(egret.TouchEvent.TOUCH_TAP, this.btnHandler, this);
    this.change_Dept.addEventListener(egret.TouchEvent.TOUCH_TAP, this.btnHandler, this);
    this.change_head.addEventListener(egret.TouchEvent.TOUCH_TAP, this.btnHandler, this);
    this.userBack.addEventListener(egret.TouchEvent.TOUCH_TAP, this.btnHandler, this);
    this.change_name.addEventListener(egret.TouchEvent.TOUCH_TAP, this.btnHandler, this);
    this.datalist.addEventListener(eui.ItemTapEvent.ITEM_TAP, this.btnHandler, this);
    this.nameBack.addEventListener(egret.TouchEvent.TOUCH_TAP, this.btnHandler, this);
    this.drop_Img.addEventListener(egret.TouchEvent.TOUCH_TAP,this.btnHandler,this);
    this.finishBtn.addEventListener(egret.TouchEvent.TOUCH_TAP, this.btnHandler, this);//修改名字完成
    this.name_input.addEventListener(egret.Event.CHANGE, this.btnHandler, this);//名字输入框的监听事件
    this.datalist.dataProvider = new eui.ArrayCollection(this.depts);
    this.datalist.itemRenderer = listLabelRenderer;

    //加载数据
    this.userInfo = await this.userHelper.getUserInfo();
    console.log(this.userInfo);
    this.username.text = this.userInfo.name;
    this.age.text = this.userInfo.age.toString();
    this.head_pic.source =Constants.HTTP_SERVER_URL + this.userInfo.avatarSrc;
    this.ID.text = this.userInfo.etreeID;
    this.dept.text = this.userInfo.dept;
    this.sex.text = this.userInfo.sex;

  
  }
  private btnHandler(evt: egret.TouchEvent) {
    let btnFocused = evt.currentTarget;
    switch (btnFocused) {
      case this.change_sex:
        if (this.sex.text == "男") {
          this.radio1.selected = true;
          this.radio2.selected = false;
        }
        else{
          this.radio1.selected = false;
          this.radio2.selected = true;
        }        
        this.sexchanging_Group.visible = true;
         this.male_Group.addEventListener(egret.TouchEvent.TOUCH_TAP, this.btnHandler, this);
        this.female_Group.addEventListener(egret.TouchEvent.TOUCH_TAP, this.btnHandler, this); 
        this.sex_changing.addEventListener(egret.TouchEvent.TOUCH_TAP, this.btnHandler_2, this);              
        break;
      case this.change_age://修改年龄
        this.agechanging_Group.visible = true;
        //this.age_input.textDisplay.text = this.age.text;
        //this.age_input.promptDisplay.text = this.age.text;
        this.agelist.selectedItem=this.age.text;
        this.age_changing.addEventListener(egret.TouchEvent.TOUCH_TAP, this.btnHandler_2, this);
        break;
      case this.change_Dept://修改院系
        this.deptchanging_Group.visible = true;
        this.dept_changing.addEventListener(egret.TouchEvent.TOUCH_TAP, this.btnHandler_2, this)
        break;
      case this.userBack:
        SceneManager.instance.removeScene(this);
        break;
      case this.change_name://修改昵称
        this.namechanging_Group.visible = true;
        this.name_input.text=this.username.text;
        break;
      case this.datalist:
        this.nowdept_label.text = this.datalist.selectedItem.label;//获取list当前item的label属性
        break;
      case this.nameBack:
        this.namechanging_Group.visible = false;
        break;
      case this.finishBtn:
        this.username.text = this.name_input.textDisplay.text;
        this.namechanging_Group.visible = false;
        break;
      case this.name_input:
        this.finishBtn.enabled = true;
        this.finishBtn.currentState = "up";
        this.finishBtn.addEventListener(egret.TouchEvent.TOUCH_TAP, this.btnHandler, this);
        break;
      case this.male_Group:
        this.radio2.selected=false;
        this.radio1.selected=true;        
        this.sex.text = "男";
        break;
      case this.female_Group:
        this.radio1.selected=false;
        this.radio2.selected = true;
        this.sex.text = "女";
        break;
      case this.drop_Img:
        if(this.drop_Img.currentState=="up")//图标朝上
          {
          this.drop_Img.currentState="down";
          this.depts_Group.visible=false;
        }
        else{
          this.drop_Img.currentState="up";
          this.depts_Group.visible=true;
        }
      break;
    }
  }
  private btnHandler_2(evt: egret.TouchEvent) {
    let btnFocused = evt.currentTarget;
    switch (btnFocused) {
      case this.sex_changing:
        this.sexchanging_Group.visible = false;
        this.saveUserInfo();
        break;   
      case this.age_changing:
        //this.age.text = this.age_input.textDisplay.text;
        this.age.text=this.agelist.selectedIndex.toString();
        this.agechanging_Group.visible = false;
        this.saveUserInfo();
        break;
      case this.dept_changing://修改院系完毕，点击空白返回
        this.dept.text = this.nowdept_label.text;
        this.deptchanging_Group.visible = false;
        this.saveUserInfo();
        break;
    }
  }
  //将修改的信息保存到数据库
  public saveUserInfo() {
    this.userHelper.updateUserInfo(this.userInfo);
  }


}